<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\teacher\Teacher;

/**
 * 文章页接口
 */
class Article extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $classList = [];
    public function _initialize()
    {
        //分类
        $this->classList = model('\app\admin\model\allclass\Allclass')->getClassList();
    }

    /**
     * 文章列表
     *
     */
    public function index()
    {   
        $data = [];

        $classid = $this->request->request('classid');//分类ID
        $page = $this->request->request('page');//分页
        $userid = $this->request->request('userid');//用户ID
        $pageNum  = 6;//每页个数

        $where = array();
        $where['status'] = 0;
        if(!empty($classid)){
            $where['classid'] = $classid;
        }

        $start = ($page-1)*$pageNum;

        //查询总页数
        $count = db('article')->where($where)->order('sort', 'desc')->count();
        $maxpages = ceil($count/$pageNum);
        if($page > $maxpages){
            $this->success('最后一页了',$data);
        }

        //文章
        $article_list = db('article')->where($where)->order('sort', 'desc')->limit($start,$pageNum)->select();
        if(!empty($article_list)){
            foreach($article_list as $key=>$value){
                $article_list[$key]['classid_title'] =model("\app\admin\model\allclass\Allclass")->get_class_name($value['classid']);//分类
                $AuthorInfo = $this->getAuthorInfo($value['author']);//作者
                $article_list[$key]['author'] = $AuthorInfo['author'];
                $article_list[$key]['headimg'] = $AuthorInfo['headimg'];

                $article_list[$key]['cover'] = getImagesUrl($value['cover']);


                $ret = $this->getfabulouscollect($userid,$value['id']);
                $article_list[$key]['is_collect'] = $ret['is_collect'];//判断是否收藏
                $article_list[$key]['is_fabulous'] = $ret['is_fabulous'];//判断是否点赞
                $article_list[$key]['addtime'] = date("Y-m-d",$value['createtime']);//创建时间
            }
        }

        //分类列表
        $data = ['maxpages'=>$maxpages,'article_list'=>$article_list];
        $this->success('请求成功',$data);
    }



    /**
     * 文章详情
     */

    public  function details()
    {
        $article_id = $this->request->request('article_id');//文章ID
        $userid = $this->request->request('userid');//用户ID

        if(empty($article_id) || empty($userid)){
            $this->error("数据错误");
        }

        //增加阅读量
        $this->addReadnum($article_id);

        //查询文章详情
        $article_info = db('article')->where('id', $article_id)->find();

        $article_info['classid_title'] = model("\app\admin\model\allclass\Allclass")->get_class_name($article_info['classid']);//分类
        $AuthorInfo = $this->getAuthorInfo($article_info['author']);//作者
        $article_info['author'] = $AuthorInfo['author'];
        $article_info['headimg'] = $AuthorInfo['headimg'];
       
        $ret = $this->getfabulouscollect($userid,$article_id);
        $article_info['is_collect'] = $ret['is_collect'];//判断是否收藏
        $article_info['is_fabulous'] = $ret['is_fabulous'];//判断是否点赞
        $article_info['addtime'] = date("Y-m-d",$article_info['createtime']);//创建时间
        $article_info['cover'] = getImagesUrl($article_info['cover']);

        //推荐文章
        $rec_list = db('article')->where(['status' => 0,'classid' => $article_info['classid']])->where('id','<>',$article_id)->order('sort', 'desc')->limit(3)->select();
        if(!empty($rec_list)){
            foreach($rec_list as $key=>$value){

                $rec_list[$key]['classid_title'] =model("\app\admin\model\allclass\Allclass")->get_class_name($value['classid']);//分类
                $AuthorInfo = $this->getAuthorInfo($value['author']);//作者
                $rec_list[$key]['author'] = $AuthorInfo['author'];
                $rec_list[$key]['headimg'] = $AuthorInfo['headimg'];
                /*
                $ret = $this->getfabulouscollect($userid,$value['id']);
                $rec_list[$key]['is_collect'] = $ret['is_collect'];//判断是否收藏
                $rec_list[$key]['is_fabulous'] = $ret['is_fabulous'];//判断是否点赞
                $rec_list[$key]['addtime'] = date("Y-m-d",$value['createtime']);//创建时间
                */

            }
        }

        $data = ['article_info'=>$article_info,'rec_list'=>$rec_list];
        $this->success("查询成功",$data);

    }


    /**
     * 增加阅读量
     */
    public function addReadnum($article_id = 0){
        
        //查询文章详情
        $article_info = db('article')->where('id', $article_id)->find();
        $readnum = $article_info['readnum']+1;

        db('article')->where('id', $article_id)->update(['readnum' => $readnum,'updatetime' => time()]);

        return true;

    }

    /**
     * 点赞或取消点赞
     */

    public function getFabulous(){

        $article_id = $this->request->request('article_id');//文章ID
        $userid = $this->request->request('userid');//用户ID

        //是否点赞
        $fabulous = db('article_fabulous')->where('user_id', $userid)->where('article_id', $article_id)->count();
        if($fabulous > 0){
            //取消点赞
            db('article_fabulous')->where('user_id', $userid)->where('article_id', $article_id)->delete();
            $msg = "取消成功";
        }else{
            //添加点赞
            $data = ['user_id' => $userid, 'article_id' => $article_id, 'createtime' => time()];
            db('article_fabulous')->insert($data);
            $msg = "点赞成功";
        }

        $this->success($msg,$fabulous);

    }


    /**
     * 收藏或取消收藏
     */

    public function getCollect(){

        $article_id = $this->request->request('article_id');//文章ID
        $userid = $this->request->request('userid');//用户ID

        //是否收藏
        $collect = db('article_collect')->where('user_id', $userid)->where('article_id', $article_id)->count();
        if($collect > 0){
            //取消收藏
            db('article_collect')->where('user_id', $userid)->where('article_id', $article_id)->delete();
            $msg = "取消成功";
        }else{
            //添加收藏
            $data = ['user_id' => $userid, 'article_id' => $article_id, 'createtime' => time()];
            db('article_collect')->insert($data);
            $msg = "收藏成功";
        }

        $this->success($msg,$collect);


    }



    /**
    * 获取文章作者 
    */
    public function getAuthorInfo($id = ""){
        
        $ret = [];

        if($id == 0){
           $author = "平台";
           $headimg = "/uploads/20210329/0de9220466f6ea2f14dde0989cd9fbb0.png";//默认图片
        }else{
            $teacher = new Teacher();
            $author = $teacher->get_teacher_nickname($id);
            $headimg = $teacher->get_teacher_headimg($id);

            if(empty($headimg)){
                $headimg = "/uploads/20210329/0de9220466f6ea2f14dde0989cd9fbb0.png";//默认图片
            }
        }

        $headimg = getImagesUrl($headimg);

        $ret = ['author'=>$author,'headimg'=>$headimg];
        return $ret;
    }


    /**
     * 判断文章是否被收藏点赞
     */

    public function getfabulouscollect($userid = "", $article_id = 0){
        
        $is_fabulous = 0;//点赞
        $is_collect = 0;//收藏

        if(empty($userid) || empty($article_id)){
            return ['is_fabulous'=>$is_fabulous,'is_collect'=>$is_collect];
        }

        //点赞
        $fabulous = db('article_fabulous')->where('user_id', $userid)->where('article_id', $article_id)->count();
        if($fabulous > 0){
            $is_fabulous = 1;
        }

        //收藏
        $collect = db('article_collect')->where('user_id', $userid)->where('article_id', $article_id)->count();
        if($collect > 0){
            $is_collect = 1;
        }

        $data = ['is_fabulous'=>$is_fabulous,'is_collect'=>$is_collect];
        return $data;

    }
}
